package com.atguigu.staservice.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.staservice.client.UcenterClient;
import com.atguigu.staservice.entity.StatisticsDaily;
import com.atguigu.staservice.mapper.StatisticsDailyMapper;
import com.atguigu.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author wxc
 * @since 2021-07-13
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    /**
     * 统计某一天的注册人数，生成统计数据
     *
     * @param day
     **/
    @Override
    public void registerCount(String day) {

        //添加记录之前删除表中相同日期的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", day);
        baseMapper.delete(wrapper);

        //远程调用得到某一天的注册人数
        R registerR = ucenterClient.countRegister(day);
        Integer countRegister = (Integer) registerR.getData().get("countRegister");

        //把获取的数据添加到统计分析数据库中
        StatisticsDaily sta = new StatisticsDaily();
        sta.setRegisterNum(countRegister);  //注册人数
        sta.setDateCalculated(day); //统计日期

        sta.setVideoViewNum(RandomUtils.nextInt(100, 200));  //视频播放
        sta.setCourseNum(RandomUtils.nextInt(20, 120));  //课程人数
        sta.setLoginNum(RandomUtils.nextInt(200, 500));  //登录人数

        baseMapper.insert(sta);

    }

    /**
     * 图表显示，返回两部分数据，日期json数组，数量json数组
     *
     * @param type
     * @param begin
     * @param end
     **/
    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        //1、根据条件，查询数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated", begin, end);
        wrapper.select("date_calculated", type);

        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);
        System.out.println(staList);

        //2、封装数据，格式： 日期 和 日期对应的数量
        //前端要求数组json结构，对应的后端java代码为list集合
        //创建两个list集合，一个日期list，一个数量list
        List<String> date = new ArrayList<>();
        List<Integer> count = new ArrayList<>();

        for (StatisticsDaily daily : staList) {
            //封装日期list集合
            date.add(daily.getDateCalculated());
            //封装对应的数量list集合
            switch (type){
                case "login_num":
                    count.add(daily.getLoginNum());
                    break;
                case "register_num":
                    count.add(daily.getRegisterNum());
                    break;
                case "video_view_num":
                    count.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    count.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }

        Map<String, Object> map = new HashMap<>();
        map.put("dateList", date);
        map.put("numList", count);

        return map;
    }
}
